package com.cl.spark.factory;

import com.cl.spark.base.BaseSparkNode;
import com.cl.spark.enums.SparkNodeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class SparkNodeFactory implements InitializingBean {
    @Autowired
    private List<BaseSparkNode> sparkNodeList;

    private static final Map<SparkNodeEnum, BaseSparkNode> SPARK_NODE_MAP = new ConcurrentHashMap<>();

    public BaseSparkNode getSparkNodeByCode(String sparkNodeCode) {
        return SPARK_NODE_MAP.get(SparkNodeEnum.getSparkNodeByCode(sparkNodeCode));
    }

    public BaseSparkNode getSparkNodeByName(String sparkNodeName) {
        return SPARK_NODE_MAP.get(SparkNodeEnum.getSparkNodeByNodeName(sparkNodeName));
    }

    @Override
    public void afterPropertiesSet() {
        for (BaseSparkNode service : sparkNodeList) {
            SPARK_NODE_MAP.put(service.getType(), service);
        }
    }
}